package com.hjy.scheduled.config.controller;

import com.alibaba.fastjson.JSON;
import com.hjy.scheduled.config.service.RedisReadWriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class redisController {

    @Autowired
    private RedisReadWriteService redis;

    @RequestMapping(value = "/findget",method = RequestMethod.POST)
    @ResponseBody
    public Object findget(String prefix,String key){
        System.out.println("........reids取数据........");
        String jsonobject = "";
        try {
            jsonobject = JSON.toJSONString(redis.get(prefix, key));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonobject;
    }


    @RequestMapping(value = "/set",method =RequestMethod.POST)
    @ResponseBody
    public Object set(String prefix,String key,String cron){
        System.out.println("......redis存数据....");
        List list = new ArrayList<>();
        Map m = new HashMap();
        m.put("id","1001");
        m.put("name","第一");
        m.put("type","1");
        m.put("cron",cron);

        list.add(m);
        prefix = "TEST:";
        key = "num";
        String val = JSON.toJSONString(list);
        try {
            redis.set(prefix, key,val);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除redis数据
     * @param prefix
     * @param key
     * @return
     */
    @RequestMapping(value = "/delete",method =RequestMethod.POST)
    public Object delete(String prefix,String key){
        try {
            redis.delete(prefix, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
